
#include <stdio.h>
#include <ctype.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include "hi_i2c.h"

int i2c_write(unsigned char device_addr , unsigned char reg_addr, unsigned int value, int reg_width, int data_width )
{
	int fd = -1;
	int ret =0;
	I2C_DATA_S i2c_data ;	

	fd = open("/dev/hi_i2c", 0);
    if(fd<0)
    {
    	printf("Open hi_i2c error!\n");
    	return -1;
    }

    printf("dev_addr:0x%2x; reg_addr:0x%2x; value:0x%2x; reg_width: %d; data_width: %d.\n", 
        device_addr, reg_addr, value, reg_width, data_width);
    i2c_data.dev_addr = device_addr ; 
    i2c_data.reg_addr = reg_addr    ; 
    i2c_data.addr_byte_num = reg_width  ; 
    i2c_data.data   = value         ; 
    i2c_data.data_byte_num = data_width ;
    
    ret = ioctl(fd, CMD_I2C_WRITE, &i2c_data);

	close(fd);
        
    return 0;
}
